<?php

if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Signup extends MY_Controller {

  public function __construct() {
    parent::__construct();
    $signuser = $this->user_session->signuser();
    if ($signuser) {
      redirect('tweets');
      return;
    }
  }

  public function index() {
    $sign_up_status = NULL;
    if ($this->is_post()) {
      $sign_up_status = 2;
      $inno_email = trim(strtolower($this->input->post('inno_email', TRUE)));
      $inno_email_confirm = trim(strtolower($this->input->post('inno_email_confirm', TRUE)));
      if (!empty($inno_email) && ($inno_email == $inno_email_confirm) && filter_var($inno_email, FILTER_VALIDATE_EMAIL)) {
        $email_data = explode('@', $inno_email);
        if (!empty($email_data) && is_array($email_data) && end(($email_data)) == 'innovaturelabs.com') {
          $sign_up_status = 3;
          $user_name = reset($email_data);
          $user_password = $this->sanitize('innovature' . rand(111, 999));
          if (!empty($user_name)) {
            $get_user_by_username = $this->user_model->get_user_by_username($user_name, FALSE);
            if (empty($get_user_by_username['result'])) {
              $set_user = $this->user_model->set_user($user_name, md5($user_password), $inno_email);
              if ($set_user['status'] == 1 && !empty($set_user['result'])) {
                $sign_up_status = 1;
                $user_id = $set_user['result']['user_id'];
                $act_token = md5($user_name . md5($user_password));
                $act_link = site_url('signup/activate/' . $user_id . '/' . $act_token);
                $log_msg = $user_name . ' (' . $inno_email . ') ' . 'from ' . $this->input->ip_address() . ' on ' . $this->agent->platform() . ' ' . $this->agent->browser() . ' ' . $this->agent->version();
                system_log('signup', $log_msg);
                $this->send_email($user_name, $user_password, $inno_email, $act_link);
              }
            } else {
              $not_activated_user = $get_user_by_username['result'];
              if ($not_activated_user['user_status'] == 0) {
                $sign_up_status = 1;
                $this->user_model->update_profile($not_activated_user['user_id'], array('user_password' => md5($user_password)));
                $act_token = md5($not_activated_user['user_name'] . md5($user_password));
                $act_link = site_url('signup/activate/' . $not_activated_user['user_id'] . '/' . $act_token);
                $log_msg = $not_activated_user['user_name'] . ' (' . $inno_email . ') ' . 'resend from ' . $this->input->ip_address() . ' on ' . $this->agent->platform() . ' ' . $this->agent->browser() . ' ' . $this->agent->version();
                system_log('signup', $log_msg);
                $this->send_email($not_activated_user['user_name'], $user_password, $inno_email, $act_link);
              }
            }
          }
        }
      } else {
        $sign_up_status = 4;
      }
      $this->session->set_flashdata('sign_up_status', $sign_up_status);
      redirect('signup');
      return;
    }
    $view_data = array();
    $this->load_view('signup', $view_data);
    return;
  }

  public function activate($user_id = NULL, $user_token = NULL) {
    $sign_up_status = 5;
    $user_id = $this->sanitize($user_id);
    $user_token = $this->sanitize($user_token);
    if (!empty($user_id) && is_numeric($user_id) && !empty($user_token)) {
      $get_user_by_id = $this->user_model->get_user_by_id($user_id, 0);
      if ($get_user_by_id['status'] == 1 && !empty($get_user_by_id['result'])) {
        $orig_token = md5($get_user_by_id['result']['user_name'] . $get_user_by_id['result']['user_password']);
        if ($user_token == $orig_token) {
          $user_update_data['user_status'] = 1;
          $update_profile = $this->user_model->update_profile($user_id, $user_update_data);
          if ($update_profile['status'] == 1 && !empty($update_profile['result'])) {
            $sign_up_status = 4;
          }
        }
      }
    }
    $this->session->set_flashdata('sign_up_status', $sign_up_status);
    redirect('signin');
    return;
  }

  private function send_email($user_name = NULL, $password = NULL, $user_email = NULL, $act_link = NULL) {
    $msg = "Username : {$user_name}<br/><br/>Password : {$password}<br/><br/>Activate : <a href='{$act_link}' target='_blank'>{$act_link}</>";
    $this->load->library('email');
    $this->email->clear(TRUE);
    $this->email->set_mailtype("html");
    $this->email->from('innotweets@gmail.com', 'innoTweets');
    $this->email->to($user_email);
    $this->email->subject('innoTweets : SignUp');
    $this->email->message($msg);
    $this->email->send();
    return;
  }

}

?>
